package com.aaa.controller;

import com.aaa.entity.Album;
import com.aaa.entity.Role;
import com.aaa.entity.Type;
import com.aaa.entity.Voice;
import com.aaa.service.AlbumService;
import com.aaa.service.RoleService;
import com.aaa.service.TypeService;
import com.aaa.service.VoiceService;
import com.aaa.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("type")
@Controller
public class TypeController {
    @Resource
    TypeService typeService;
    @Resource
    VoiceService voiceService;
    @Resource
    AlbumService albumService;

    @RequestMapping("selectType")
    @ResponseBody
    public PageInfo select(Integer pageNum, Integer pageSize, String tname) {
        if(tname!=null){
            //模糊查询根据类型名
            PageHelper.startPage(pageNum,pageSize);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.like("tname",tname);
            List<Type> list=typeService.list(queryWrapper);
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Type> list=typeService.list();
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }
    }

    //批量删除
    @RequestMapping("delType")
    @ResponseBody
    public boolean delType(String str) {
        String[] tid= str.split(",");
        List<String> tidList=new ArrayList<>();
        for (int i=0;i<tid.length;i++){
            tidList.add(tid[i]);
        }
        boolean b=false;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("typeid",tidList);
        List<Album> albumList = albumService.list(queryWrapper);
        if(albumList.size()==0){
            for (int i=0;i<tid.length;i++) {
                b = typeService.removeById(tid[i]);
            }
        }
        return b;
    }
    //类型修改
    @RequestMapping("updType")
    @ResponseBody
    public boolean updType(Type type) {
        boolean b = typeService.updateById(type);
        return true;
    }
    //音频添加(根据添加音频返回的主键进行修改)
    @RequestMapping("typeAdd")
    @ResponseBody
    public boolean typeAdd(Type type) {
        return typeService.save(type);
    }

}
